<template>
  <el-container>
    <el-aside>
      <div
        class="list_item"
        v-for="(item, index) in lists"
        :key="index"
        @dragstart="dragStart($event, index)"
        draggable="true"
        :ref="setItemRef"
      >
        {{ item.id }}
        {{ item.name }}
        {{ item.sex }}
      </div>
      <button @click="addList">增加</button>
    </el-aside>
    <el-main>
      <div
        ref="contentRef"
        class="content_box"
        @dragover.prevent="dragOver"
        @drop="drop"
        @dragleave="dragLeave"
      ></div>
    </el-main>
  </el-container>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
let lists = reactive([
  {
    id: 1,
    name: "summer",
    sex: "女",
  },
  {
    id: 2,
    name: "小雪",
    sex: "女",
  },
]);
let dragLists = reactive<any[]>([]);

let contentRef = ref();
let dragItem = ref();
// 存储DOM元素的引用
const itemRefs = ref<any>([]);

// 设置引用的函数
const setItemRef = (el) => {
  itemRefs.value.push(el);
};
const dragStart = (e, index) => {
  dragItem.value = itemRefs.value[index]
};

const dragOver = (e) => {
  contentRef.value.style.border = "1px dashed #ccc";
};
const dragLeave = (e) => {
  contentRef.value.style.border = "none";
};
const drop = (e) => {
  contentRef.value.append(dragItem.value);
};

const addList = () => {
    lists.push({
        id: lists.length + 1,
        name: 'hhh',
        sex:'男'
    })
}
</script>

<style lang="less" scoped>
.list_item {
  width: 200px;
  border-bottom: 1px solid #ccc;
  color: #fff;
  text-align: center;
  line-height: 40px;
  background: #ccc;
  margin: 10px;
  cursor: move;
}
.content_box {
  width: 400px;
  height: 500px;
}
</style>
